import Vue from 'vue'
import VueRouter from 'vue-router'
// 导入需要的组件
// import Login from '@/components/Login.vue'
// import Home from '@/components/Home.vue'
// import Welcome from '@/components/Welcome.vue'
// import User from '@/components/User/User.vue'
// import Rights from '@/components/Power/Rights.vue'
// import Roles from '@/components/Power/Roles.vue'
// import Cate from '@/components/Goods/Cate.vue'
// import Params from '@/components/Goods/Params.vue'
// import List from '@/components/Goods/List.vue'
// import Add from '@/components/Goods/Add.vue'
// import Order from '@/components/Order/Order.vue'
// import Report from '@/components/Report/Report.vue'

/* 对路由进行分组打包 */
const Login = () => import(/* webpackChunkName: "login_home_welome" */ '@/components/Login.vue')
const Home = () => import(/* webpackChunkName: "login_home_welome" */ '@/components/Home.vue')
const Welcome = () => import(/* webpackChunkName: "login_home_welome" */ '@/components/Welcome.vue')

const User = () => import(/* webpackChunkName: "Users_Rights_Roles" */ '@/components/User/User.vue')
const Rights = () => import(/* webpackChunkName: "Users_Rights_Roles" */ '@/components/Power/Rights.vue')
const Roles = () => import(/* webpackChunkName: "Users_Rights_Roles" */ '@/components/Power/Roles.vue')

const Cate = () => import(/* webpackChunkName: "Cate_Params" */ '@/components/Goods/Cate.vue')
const Params = () => import(/* webpackChunkName: "Cate_Params" */ '@/components/Goods/Params.vue')

const List = () => import(/* webpackChunkName: "GoodsList_Add" */ '@/components/Goods/List.vue')
const Add = () => import(/* webpackChunkName: "GoodsList_Add" */ '@/components/Goods/Add.vue')

const Order = () => import(/* webpackChunkName: "Order_Report" */ '@/components/Order/Order.vue')
const Report = () => import(/* webpackChunkName: "Order_Report" */ '@/components/Report/Report.vue')

Vue.use(VueRouter)

const routes = [
  // 访问'/'，重定向到login
  { path: '/', redirect: '/login' },
  { path: '/login', component: Login },
  {
    path: '/home',
    component: Home,
    redirect: '/welcome',
    children: [
      { path: '/welcome', component: Welcome },
      { path: '/users', component: User },
      { path: '/rights', component: Rights },
      { path: '/roles', component: Roles },
      { path: '/categories', component: Cate },
      { path: '/params', component: Params },
      { path: '/goods', component: List },
      { path: '/goods/add', component: Add },
      { path: '/orders', component: Order },
      { path: '/reports', component: Report }

    ]
  }

]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next()
  const token = sessionStorage.getItem('token')
  if (!token) {
    // token不存在
    return next('/login')
  }
  next()
})
export default router
